﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGameAula02
{
    class PlayerManager
    {

        public Player mainPlayer;
        //Player backUp;
        List<Player> PlayerList;
        Texture2D p;
        Texture2D s;
        Animation[] a;
        Animation[] sa;
        ShotManager sm;

        public PlayerManager(Texture2D player, Texture2D shot, Vector2 position, Animation[] animations, Animation[] shotanimation)
        {
            p = player;
            s = shot;
            a = animations;
            sa = shotanimation;
            PlayerList = new List<Player>();

            mainPlayer = new Player(player, shot, position, animations);
            mainPlayer.Origin = new Vector2(mainPlayer.Origin.X, 0);

            PlayerList.Insert(0, mainPlayer);

            sm = new ShotManager(s, shotanimation);
            sm.ShotLifeTime = 3 * 1000;
            sm.timeBetweenShots = 250f;

        }

        public void Update(GameTime gt, Vector2 wb)
        {
            mainPlayer = PlayerList[0];
            //Position = mainPlayer.Position;
            //if (backUp != null) backUp.Update(gt); 
            foreach (Player p in PlayerList)
            {
                p.Update(gt);
            }

            if (Input.GetKey(Keys.Space))
            {
                sm.NewShot(gt, PlayerList[0], EntityType.Friendly);
            }
            sm.Update(gt);

            CheckBonds(wb);

            //Predicate<Player> pp;
            //Level1.SceneObjects 

            Level1.SceneObjects.RemoveAll(SceneObjectType);

            foreach (Player p in PlayerList)
            {
                Level1.SceneObjects.Add(p);
            }            
        }
        public bool SceneObjectType(AnimatedObject2D ao)
        {
            return typeof (Player) == ao.GetType();
        }
        //private static bool MatchPlayer(AnimatedObject2D ao)
        //{
        //    return typeof(ao as Player );
        //}

        public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
           // mainPlayer.Draw(spriteBatch, gameTime);
            //if (backUp != null) backUp.Draw(spriteBatch, gameTime);
            foreach (Player p in PlayerList)
            {
                p.Draw(spriteBatch, gameTime);
            }
            sm.Draw(spriteBatch, gameTime);
        }


        private void CheckBonds(Vector2 wB)
        {
            bool touchingBorder;
            bool outOfBorder;
            float playerX, playerY;
            Player newPlayer;

            if (PlayerList.Count == 1)
            {
               
                    touchingBorder = PlayerList[0].Position.X < 0 ||
                                     PlayerList[0].Position.X + PlayerList[0].Rect.Width > wB.X ||
                                     PlayerList[0].Position.Y < 0 ||
                                     PlayerList[0].Position.Y + PlayerList[0].Rect.Height > wB.Y;
               
                if (touchingBorder)
                {
                    playerX = PlayerList[0].Position.X;
                    playerY = PlayerList[0].Position.Y;

                    if (PlayerList[0].Position.X < 0)
                    {
                        playerX = wB.X - 1;
                    }
                    else if (PlayerList[0].Position.X + PlayerList[0].Rect.Width > wB.X)
                    {
                        playerX = 1 - PlayerList[0].Rect.Width;
                    }
                    if (PlayerList[0].Position.Y + PlayerList[0].Rect.Height > wB.Y)
                    {
                        playerY = 1 - PlayerList[0].Rect.Height;
                    }
                    else if (PlayerList[0].Position.Y < 0)
                    {
                        playerY = wB.Y - 1;
                    }

                    newPlayer = new Player(p, s, PlayerList[0].Position, a);
                    newPlayer.Origin = PlayerList[0].Origin;
                    newPlayer.Position = new Vector2(playerX, playerY);
                    newPlayer.Rotation = PlayerList[0].Rotation;
                    newPlayer.Speed = PlayerList[0].Speed;

                    PlayerList.Insert(0, newPlayer);
                }
            }

            for (int i = PlayerList.Count - 1; i >= 0; i--)
            {
                outOfBorder = PlayerList[i].Position.X - PlayerList[i].Rect.Width > wB.X ||
                              PlayerList[i].Position.X + PlayerList[i].Rect.Width < 0 ||
                              PlayerList[i].Position.Y - PlayerList[i].Rect.Height > wB.Y ||
                              PlayerList[i].Position.Y + PlayerList[i].Rect.Height < 0;

                if (outOfBorder)
                {
                    if (i > 0 || i == 0 && PlayerList.Count > 1)
                        PlayerList.RemoveAt(i);
                }

            }
        }

        private bool InsideBounds(Rectangle b, Vector2 p)
        {
            return p.X > b.X && p.X < b.Width && p.Y > b.Y && p.Y < b.Height;
        }


    }
}
